//
//  NSDictionary+Extend.m
//  XLDoctor
//
//  Created by longma on 2018/12/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "UITextField+MJUI.h"

@implementation UITextField (MJUI)

@dynamic mj_titleColorName;
- (void)setMj_titleColorName:(NSString *)mj_titleColorName {
    self.textColor = [UIColor mj_colorName:mj_titleColorName];
}


@dynamic mj_disableThirdPartyKeyboard;
- (void)setMj_disableThirdPartyKeyboard:(BOOL)mj_disableThirdPartyKeyboard {
    if(mj_disableThirdPartyKeyboard){
        [MJHelper disableThirdPartyKeyboardWithUITextField:self];
    }
}

@dynamic mj_limitedNumber;
- (void)setMj_limitedNumber:(NSInteger)mj_limitedNumber {
    MJWeakSelf
    [[RACSignal merge:@[self.rac_textSignal, RACObserve(self, text)]] subscribeNext:^(NSString* text){
        if (mj_limitedNumber>0 && text.length > mj_limitedNumber) {
            //截取未超出部分字符
            weakSelf.text = [text substringToIndex:mj_limitedNumber];
        }
    }];
}

@dynamic mj_placeholderColorName;
- (void)setMj_placeholderColorName:(NSString *)placeholderColorName {
    
    self.mj_placeholderColor = [UIColor mj_colorName:placeholderColorName];
}

@dynamic mj_placeholderColor;
- (void)setMj_placeholderColor:(UIColor *)mj_placeholderColor {
    if(self.placeholder){
        //  绘制placeholder字体
        NSMutableAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: mj_placeholderColor}];
        //  设置placeholder字体
        self.attributedPlaceholder = placeholderText;
    }
}




//- (void)mj_limitedNumber:(NSInteger)limitedNumber {
//    MJWeakSelf
//    [[RACSignal merge:@[self.rac_textSignal, RACObserve(self, text)]] subscribeNext:^(NSString* text){
//        if (limitedNumber>0 && text.length > limitedNumber) {
//            //截取未超出部分字符
//            weakSelf.text = [text substringToIndex:limitedNumber];
//        }
//    }];
//
//}

- (void)mj_shouldChangeString:(NSString *)aString {
    MJWeakSelf
    [[self rac_textSignal] subscribeNext:^(NSString * _Nullable text) {
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:aString] invertedSet];
        NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        //filtered 过滤后的（其他字符会被""替换）
        weakSelf.text = filtered;
        
    }];
}

- (void)mj_ignoreString:(NSString *)aString {
    MJWeakSelf
    [[self rac_textSignal] subscribeNext:^(NSString * _Nullable text) {
        
        NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:aString];//***
        NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        weakSelf.text = filtered;
        
    }];
}

- (void)mj_setClearButtonWithImage:(UIImage *)image {
    UIButton *userNameClearButton = [self valueForKey:@"_clearButton"];
    [userNameClearButton setImage:image forState:UIControlStateNormal];
}

- (void)mj_becomeFirstResponder {
    MJUIAfter_GCD(0.6, ^{
        [self becomeFirstResponder];
    });
}


@end

/// 语言
@implementation UITextField (Language)

@dynamic mj_pLocalizable;
- (void)setMj_pLocalizable:(NSString *)mj_pLocalizable {
    MJWeakSelf
    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
        weakSelf.placeholder = MJUILocalizedString(mj_pLocalizable);
    }];
}

@dynamic mj_localizable;
- (void)setMj_localizable:(NSString *)mj_localizable {
    MJWeakSelf
    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
        weakSelf.text = MJUILocalizedString(mj_localizable);
    }];

}
@end
